class Solution(object):
    def findClosestElements(self, arr, k, x):
        """
        :type arr: List[int]
        :type k: int
        :type x: int
        :rtype: List[int]
        """
        n = len(arr)
        i = 0
        while i < n - k:
            if abs(arr[i + k] - x) < abs(arr[i] - x):
                i += 1
            else:
                if abs(arr[i + k] - x) == abs(arr[i] - x) and arr[i] == arr[i + k]:
                    i += 1
                else:
                    break
        return arr[i: i + k]


data = Solution()
arr = [1, 2, 3, 4, 5]
k = 4
x = 3
print(data.findClosestElements(arr, k, x))